﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingClientsWebsiteManager.Espanol.Pages
{
    public partial class Groups : Telerik.Web.UI.RadAjaxPage
    {
        private void GetAllGroupByUsername(string Username)
        {
            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.UsersByGroups> usersByGroupsEntites = GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.GetByUsername(Username);
          //  if (usersByGroupsEntites.Count > 0)  usersByGroupsEntites.RemoveAt(0);
            GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.DeepLoad(usersByGroupsEntites, true, GPSTrackingServerDB.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingServerDB.Entities.Groups));

            GroupGrid.DataSource = usersByGroupsEntites;
        }

        private void Delete_Group(int GroupId)
        {
            

        }       

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GroupGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetAllGroupByUsername(Request.QueryString["Username"]);
        }        

        protected void GroupGrid_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GridEditableItem Item = e.Item as GridEditableItem;
                int GroupID = Convert.ToInt32(Item.GetDataKeyValue("GroupID"));
                Hashtable values = new Hashtable();
                Item.ExtractValues(values);

                GPSTrackingServerDB.Entities.Groups groupEntity = GPSTrackingServerDB.Data.DataRepository.GroupsProvider.GetByGroupId(GroupID);

                groupEntity.GroupId = GroupID;
                groupEntity.GroupName = values["GroupIdSource.GroupName"].ToString();

                GPSTrackingServerDB.Data.DataRepository.GroupsProvider.Update(groupEntity);

                //Refresh
                GetAllGroupByUsername(Request.QueryString["Username"]);
                GroupGrid.DataBind();

                RadNotification1.Show();
            }
            catch (Exception ex)
            {
                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_miniError('" + ex.Message + "','Grupos error ...');", true);
            }
        }

        protected void GroupGrid_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GridDataItem Item = e.Item as GridDataItem;
            int GroupID = Convert.ToInt32(Item.GetDataKeyValue("GroupID"));
            string Username = Request.QueryString["Username"];
            GPSTrackingServerDB.Data.TransactionManager transactionManager = GPSTrackingServerDB.Data.DataRepository.Provider.CreateTransaction();

            try
            {
                transactionManager.BeginTransaction();

                //Imei
                GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GroupsByImei> groupsByImeiEntites = GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.GetByGroupId(GroupID);
                foreach (GPSTrackingServerDB.Entities.GroupsByImei entity in groupsByImeiEntites)
                {
                    entity.OriginalGroupId = entity.GroupId;
                    entity.OriginalImei = entity.Imei;

                    entity.GroupId = 1; //UnCategory
                    entity.EntityState = GPSTrackingServerDB.Entities.EntityState.Changed;


                    GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.Update(transactionManager, entity);
                }

                //Delete Groups by User
                GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.Delete(transactionManager, Username, GroupID);


                //Delete group name
                GPSTrackingServerDB.Data.DataRepository.GroupsProvider.Delete(transactionManager, GroupID);

                transactionManager.Commit();

                //Refresh
                GetAllGroupByUsername(Username);
                GroupGrid.DataBind();
            }
            catch (Exception ex)
            {
                transactionManager.Rollback();
                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_miniError('" + ex.Message + "','Grupos error ...');", true);
            }
        }

        protected void GroupGrid_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GPSTrackingServerDB.Data.TransactionManager transactionManager = GPSTrackingServerDB.Data.DataRepository.Provider.CreateTransaction();
            try
            {
                transactionManager.BeginTransaction();

                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable values = new Hashtable();
                item.ExtractValues(values);                

                //Insert Group
                GPSTrackingServerDB.Entities.Groups groupEntity = new GPSTrackingServerDB.Entities.Groups();
                groupEntity.GroupName = values["GroupIdSource.GroupName"].ToString();

                GPSTrackingServerDB.Data.DataRepository.GroupsProvider.Insert(transactionManager, groupEntity);

                //Inser Group by User
                GPSTrackingServerDB.Entities.UsersByGroups usersByGroupsEntity = new GPSTrackingServerDB.Entities.UsersByGroups();
                usersByGroupsEntity.Username = Request.QueryString["Username"];
                usersByGroupsEntity.GroupId = groupEntity.GroupId;

                GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.Insert(transactionManager,usersByGroupsEntity);

                transactionManager.Commit();

                //Refresh
                GetAllGroupByUsername(Request.QueryString["Username"]);
                GroupGrid.DataBind();

                RadNotification1.Show();

            }
            catch (Exception ex)
            {
                transactionManager.Rollback();
                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_miniError('" + ex.Message + "','Grupos error ...');", true);
            }

        }
    }
}